package com.zigong.app.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.zigong.app.R;
import com.zigong.app.beans.FProgram;
import com.zigong.app.ui.utils.GlideUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/6/28 0028.
 */

public class LookBackAdapter extends BaseExpandableListAdapter {
    private List<FProgram> al_title;
    private Context context;
    private ChoiceDateCallBack choiceDateCallBack;

    public ChoiceDateCallBack getChoiceDateCallBack() {
        return choiceDateCallBack;
    }

    public LookBackAdapter setChoiceDateCallBack(ChoiceDateCallBack choiceDateCallBack) {
        this.choiceDateCallBack = choiceDateCallBack;
        return this;
    }

    public LookBackAdapter(List<FProgram> al_title, Context context) {
        this.al_title = al_title;
        // this.al_conetnt = al_conetnt;
        this.context = context;
    }

    public void setData(List<FProgram> al_title) {
        this.al_title = al_title;
        notifyDataSetChanged();
    }

    public void addAll(List<FProgram> al_title) {
        this.al_title.addAll(al_title);
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return al_title.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return al_title.get(groupPosition).getProgrammeList() == null ? 0 : al_title.get(groupPosition).getProgrammeList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return al_title.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return al_title.get(groupPosition).getProgrammeList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View titleView = LayoutInflater.from(context).inflate(R.layout.item_look_back_title, parent, false);
//        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT, 150);
//        titleView.setLayoutParams(lp);
        CustomFontTextView title_cv = (CustomFontTextView) titleView.findViewById(R.id.item_look_back_title);
        CustomFontTextView date_cv = (CustomFontTextView) titleView.findViewById(R.id.item_look_back_date);
        View view = titleView.findViewById(R.id.item_look_back_title_divider);

        date_cv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (choiceDateCallBack != null) {
                    choiceDateCallBack.getDateCallBack(groupPosition, al_title.get(groupPosition).getProgrammeUrl());
                }
            }
        });


        ImageView choice_iv = (ImageView) titleView.findViewById(R.id.item_look_back_down);
        title_cv.setText(!TextUtils.isEmpty(al_title.get(groupPosition).getProgrammeName()) ? al_title.get(groupPosition).getProgrammeName() : "");
        if (isExpanded) {
            choice_iv.setImageResource(R.mipmap.icon_anchor_right);
            choice_iv.setRotation(270f);
            date_cv.setVisibility(View.VISIBLE);
        } else {
            choice_iv.setImageResource(R.mipmap.icon_anchor_right);
            choice_iv.setRotation(90f);
            if (groupPosition == al_title.size() - 1) {
                view.setVisibility(View.GONE);
            } else {
                view.setVisibility(View.VISIBLE);
            }
        }
        return titleView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ContentHolder contentHolder;
        if (convertView == null) {
            contentHolder = new ContentHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_look_back_content, parent, false);
            contentHolder.look_back_ll = (LinearLayout) convertView.findViewById(R.id.item_look_back_content_ll);
            contentHolder.title_cv = (CustomFontTextView) convertView.findViewById(R.id.item_look_back_content_title);
            contentHolder.time_cv = (CustomFontTextView) convertView.findViewById(R.id.item_look_back_content_time);
            contentHolder.play_count_cv = (CustomFontTextView) convertView.findViewById(R.id.item_look_back_content_play_count);
            contentHolder.img_cv = (CustomEXImageView) convertView.findViewById(R.id.item_look_back_content_img);
            contentHolder.news_type = (ImageView) convertView.findViewById(R.id.item_look_back_content_type);
            contentHolder.play_length = (CustomFontTextView) convertView.findViewById(R.id.item_look_back_content_length);
            contentHolder.divider = (View) convertView.findViewById(R.id.item_look_back_content_divider);
            convertView.setTag(contentHolder);
        } else {
            contentHolder = (ContentHolder) convertView.getTag();
        }

        if (groupPosition == al_title.size() - 1 && childPosition == al_title.get(groupPosition).getProgrammeList().size() - 1) {
            contentHolder.divider.setVisibility(View.GONE);
        } else {
            contentHolder.divider.setVisibility(View.VISIBLE);
        }

        String title = al_title.get(groupPosition).getProgrammeList().get(childPosition).getProgrammeTitle();
        String time = al_title.get(groupPosition).getProgrammeList().get(childPosition).getPubTime();
        String playCount = al_title.get(groupPosition).getProgrammeList().get(childPosition).getPlayTotal();
        String img = al_title.get(groupPosition).getProgrammeList().get(childPosition).getProgrammeImage();
        String playLength = al_title.get(groupPosition).getProgrammeList().get(childPosition).getProgrammeLength();
        boolean isSelect = al_title.get(groupPosition).getProgrammeList().get(childPosition).isSelect();
        contentHolder.title_cv.setText(!TextUtils.isEmpty(title) ? title : "");
        contentHolder.time_cv.setText(!TextUtils.isEmpty(time) ? time : "");
        if (!TextUtils.isEmpty(playLength)) {
            contentHolder.play_length.setVisibility(View.VISIBLE);

            contentHolder.play_length.setText(playLength);
        } else {
            contentHolder.play_length.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(playCount) && !TextUtils.equals("0", playCount)) {
            contentHolder.play_count_cv.setVisibility(View.VISIBLE);
            contentHolder.play_count_cv.setText("播放" + playCount + "次");
        }
        if (!TextUtils.isEmpty(img)) {
            GlideUtil.loadImgSmall169Def(context, img, contentHolder.img_cv);
        } else {
            contentHolder.img_cv.setImageResource(R.mipmap.icon_def_16_9_s);
        }
        if (isSelect) {
            contentHolder.news_type.setImageResource(R.mipmap.icon_home_onair);
        } else {
            contentHolder.news_type.setImageResource(R.mipmap.icon_home_play);
        }

//        contentHolder.look_back_ll.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (choiceDateCallBack != null) {
//                    choiceDateCallBack.getItemClick(groupPosition, childPosition);
//                }
//            }
//        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ContentHolder {
        LinearLayout look_back_ll;
        CustomFontTextView title_cv, time_cv, play_count_cv, play_length;
        CustomEXImageView img_cv;
        ImageView news_type;
        View divider;
    }

    public interface ChoiceDateCallBack {
        void getDateCallBack(int position, String url);

        void getItemClick(int groupPosition, int childPosition);
    }
}
